#!/bin/sh
# Copyright 2014-2016 BitMover, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


obk=bk     # original bk for comparison
#obk=$HOME/bk/bk-dev/src/bk
nbk=../bk  # this new bk

check()
{
	cmp -s $1 $2
	if [ $? -eq 0 ]; then
		echo -n 0
	else
		grep -q '^<<<' $2
		if [ $? -eq 0 ]; then
			echo -n 1
		else
			echo -n 2
		fi
	fi
}

trap "rm -f userfile P M G myfile myfile2 mergefile sccsfile" 0

for f in $*
do	bk prs -hnd'$if(:MERGE:){:REV: :PARENT: :MPARENT: :GCA2:}' $f |
	while read r p m gca
	do	
		args="-g -l$p -r$m $f"
		$obk smerge $args > myfile
		$nbk smerge $args > myfile2

		# skip merges were installed smerge is the same
		cmp -s myfile myfile2 && continue
		
		bk get -qkpr$r $f > userfile
		
		check myfile myfile2
		check userfile myfile
		check userfile myfile2

		# check diff3
		bk get -qkpr$p $f > P
		bk get -qkpr$m $f > M
		bk get -qkpr$gca $f > G
		bk merge P G M mergefile
		check userfile mergefile
		rm -f G P M mergefile

		# check sccsmerge
		bk get -qkp -M$m -r$p $f > sccsfile
		check userfile sccsfile
		rm -f sccsfile

		echo '' smerge $args

		xxdiff myfile myfile2 userfile

		rm -f myfile myfile2 userfile
	done
done


